package com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.test;

import com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.db.SpitterRepository;
import com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.db.SpittleRepository;
import com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.db.hibernate5.HibernateSpitterRepository;
import com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.db.hibernate5.HibernateSpittleRepository;
import org.hibernate.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.TransactionManagementConfigurer;

import javax.inject.Inject;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableTransactionManagement
@ComponentScan({"com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.db.*"})
public class RepositoryTestConfig implements TransactionManagementConfigurer {

  @Inject
  private SessionFactory sessionFactory;

  @Bean
  public DataSource dataSource() {
    EmbeddedDatabaseBuilder edb = new EmbeddedDatabaseBuilder();
    edb.setType(EmbeddedDatabaseType.H2);
    edb.addScript("classpath:SpringInAction4th/chapter11/hibernate5/schema.sql");
    edb.addScript("classpath:SpringInAction4th/chapter11/hibernate5/test-data.sql");
    EmbeddedDatabase embeddedDatabase = edb.build();
    return embeddedDatabase;
  }

  public PlatformTransactionManager annotationDrivenTransactionManager() {
    System.out.println(sessionFactory);
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    transactionManager.setSessionFactory(sessionFactory);
    return transactionManager;
  }

  @Bean
  public SpitterRepository spitterRepository(SessionFactory sessionFactory) {
    return new HibernateSpitterRepository(sessionFactory);
  }

  @Bean
  public SpittleRepository spittleRepository(SessionFactory sessionFactory) {
    return new HibernateSpittleRepository(sessionFactory);
  }
  @Bean
  public SessionFactory sessionFactoryBean() {
    try {
      LocalSessionFactoryBean lsfb = new LocalSessionFactoryBean();
      lsfb.setDataSource(dataSource());
      lsfb.setPackagesToScan("com.java.tutorials.spring.springInAction4th.chapter11.hibernate5.domain");
      Properties props = new Properties();
      props.setProperty("dialect", "org.hibernate.dialect.H2Dialect");
      lsfb.setHibernateProperties(props);
      lsfb.afterPropertiesSet();
      SessionFactory object = lsfb.getObject();
      return object;
    } catch (Exception e) {
      System.out.println("---------------------------------");
      e.printStackTrace();
      return null;
    }
  }
}
